<script setup>
  import { ref } from 'vue'
  import { useTaskStore } from '@/stores/task'

  const taskStore = useTaskStore()

  //接受父组件传递过来的数据
  const props = defineProps({
    types: Array,
    dataKey: String,
  })

  //选中状态绑定
  const typeIndex = ref(null)
  function onOptionsSelect(index, text, id) {
    // 修改状态
    typeIndex.value = index
    taskStore.recordData[props.dataKey] = id
    // console.log(props.dataKey)
    // console.log(text)
  }
</script>
s
<template>
  <view class="vehicle-options">
    <view
      v-for="(option, index) in props.types"
      :class="{ active: typeIndex === index }"
      :key="option.id"
      class="option"
      @click="onOptionsSelect(index, option.text, option.id)"
    >
      {{ option.text }}
    </view>
  </view>
</template>

<style lang="scss" scoped>
  .vehicle-options {
    display: flex;
    flex-wrap: wrap;
    font-size: $uni-font-size-small;

    .option {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 180rpx;
      height: 70rpx;
      padding-top: 1rpx;
      margin-top: 30rpx;
      margin-right: 38rpx;
      color: $uni-secondary-color;
      border: 2rpx solid $uni-bg-color;
      background-color: $uni-bg-color;
      border-radius: 20rpx;

      &:nth-child(3n) {
        margin-right: 0;
      }

      &.active {
        color: $uni-primary;
        border: 2rpx solid $uni-primary;
        background-color: #ffe0dd;
      }
    }
  }
</style>
